package com.tianquan.PaginationPlugin;/**
 * @author starsea
 * @date 2024-09-28 下午7:38
 */

import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ResultMap;
import org.apache.ibatis.mapping.ResultMapping;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *@BelongsProject: uploaddemo
 *@BelongsPackage: com.tianquan.PaginationPlugin
 *@Author: tianquan
 *@CreateTime: 2024-09-28  19:38
 *@Description: 测试
 *@Version: 1.0
 */
public abstract class AbstractPage {


    private static final ThreadLocal<Page> LOCAL_PAGE = new ThreadLocal<>();

    private static final ThreadLocal<PageInfo<?>> LOCAL_PAGEINFO = new ThreadLocal<>();

    protected static final List<ResultMapping> EMPTY_RESULTMAPPING = new ArrayList<>(0);

    protected static final String COUNT = "_COUNT";

    public  void setPage(Page page) {
        LOCAL_PAGE.set(page);
    }

    public  Page getPage() {
        return LOCAL_PAGE.get();
    }

    public void setPgeInfo(PageInfo pageInfo) {
        LOCAL_PAGEINFO.set(pageInfo);
    }

    public PageInfo getPageInfo() {
        return LOCAL_PAGEINFO.get();
    }


    protected  MappedStatement newCountMappedStatement(MappedStatement ms, String newMsId) {
        MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), newMsId, ms.getSqlSource(), ms.getSqlCommandType());
        builder.resource(ms.getResource());
        builder.fetchSize(ms.getFetchSize());
        builder.statementType(ms.getStatementType());
        builder.keyGenerator(ms.getKeyGenerator());
        if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) {
            StringBuilder keyProperties = new StringBuilder();
            for (String keyProperty : ms.getKeyProperties()) {
                keyProperties.append(keyProperty).append(",");
            }
            keyProperties.delete(keyProperties.length() - 1, keyProperties.length());
            builder.keyProperty(keyProperties.toString());
        }
        builder.timeout(ms.getTimeout());
        builder.parameterMap(ms.getParameterMap());
        // count查询返回值int
        List<ResultMap> resultMaps = new ArrayList<>();
        ResultMap resultMap = new ResultMap.Builder(ms.getConfiguration(), ms.getId(), Long.class, EMPTY_RESULTMAPPING).build();
        resultMaps.add(resultMap);
        builder.resultMaps(resultMaps);
        builder.resultSetType(ms.getResultSetType());
        builder.cache(ms.getCache());
        builder.flushCacheRequired(ms.isFlushCacheRequired());
        builder.useCache(ms.isUseCache());

        return builder.build();
    }

    protected static Long executeAutoCount(Executor executor, MappedStatement countMs,
                                  Object parameter, BoundSql boundSql,
                                  RowBounds rowBounds, ResultHandler resultHandler) throws IllegalAccessException, SQLException {
        // 创建 count 查询的缓存 key
        CacheKey countKey = executor.createCacheKey(countMs, parameter, RowBounds.DEFAULT, boundSql);
        // 执行 count 查询
        Object countResultList = executor.query(countMs, parameter, RowBounds.DEFAULT, resultHandler, countKey, boundSql);
        Long count = (Long) ((List) countResultList).get(0);
        return count;
    }


}
